package com.tgsoft.kdcloud.controller;


import cn.hutool.core.convert.Convert;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import com.kingdee.bos.webapi.entity.RepoRet;
import com.kingdee.bos.webapi.sdk.K3CloudApi;

import com.tgsoft.common.core.util.R;
//import com.tgsoft.kdcloud.enums.CarStatusEnum;
import com.tgsoft.kdcloud.enums.MoStatusEnum;
import com.tgsoft.kdcloud.vo.InvVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;


@RestController
@Tag(name = "拆解车业务")
@Slf4j
public class CarInfoController {

    @Autowired
    private K3CloudApi k3CloudApi;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private Gson gson;

    @PostMapping("/car_sheet_code")
    @Operation(summary = "2查询新收车")
    public R getPurCar(@RequestBody Map<String,String> jsonStirng){

        String carStatus = jsonStirng.get("sheet_code");


        //按照 库存状态 未检销 查找即时库存
        String invJson ="{\n" +
                "    \"FormId\": \"STK_Inventory\",\n" +
                "    \"FieldKeys\": \"FMaterialId.fnumber,FStockStatusId.FNumber,FMaterialName,FStockId,FModel,FBaseQty,FLot.Fnumber\",\n" +
                "    \"FilterString\": \"FStockStatusId.FNumber in ("+carStatus+")  and FBASEQTY !=0 and FLot.Fnumber is not null\",\n" +
                "    \"OrderString\": \"\",\n" +
                "    \"TopRowCount\": 0,\n" +
                "    \"StartRow\": 0,\n" +
                "    \"Limit\": 0\n" +
                "}";


        String result = null;
        try {
            result = k3CloudApi.executeBillQueryJson(invJson);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return R.ok(result,"");
    }


    @PostMapping("/car_info")
    @Operation(summary = "3按批次查询明细")
    public R getCarInfo(@RequestBody Map<String,String> jsonStirng){

        String lotno = jsonStirng.get("sheet_code");

        ObjectNode rtobj = objectMapper.createObjectNode();
        try {

            String carStatus = jsonStirng.get("sheet_code");


            //按照 库存状态 未检销 查找即时库存 的库存状态
            String invJson ="{\n" +
                    "    \"FormId\": \"STK_Inventory\",\n" +
                    "    \"FieldKeys\": \"FMaterialId.fnumber,FStockStatusId.fnumber,FStockId.fnumber," +
                    "FStockUnitId.fnumber,FStockUnitId.fname,FBASEQTY,FLot.Fnumber\",\n" +
                    "    \"FilterString\": \"FLot.FNumber='"+lotno+"' and FBASEQTY !=0\",\n" +
                    "    \"OrderString\": \"\",\n" +
                    "    \"TopRowCount\": 0,\n" +
                    "    \"StartRow\": 0,\n" +
                    "    \"Limit\": 0\n" +
                    "}";

            var  invresult11 = k3CloudApi.executeBillQuery(invJson);




            // 一 、按照 批号 查询采购入库单列表
            String billNoQuery = "{\n" +
                    "    \"FormId\": \"STK_InStock\",\n" +
                    "    \"FieldKeys\": \"FBillNo\",\n" +
                    // 所有状态的车都查
                    // "    \"FilterString\": \"FStockStatusId.FNumber='KCZT005' and FLot.FNumber='"+lotno+"'\",\n" +
                    "    \"FilterString\": \"FLot.FNumber='"+lotno+"'\",\n" +
                    "    \"OrderString\": \"\",\n" +
                    "    \"TopRowCount\": 0,\n" +
                    "    \"StartRow\": 0,\n" +
                    "    \"Limit\": 0\n" +
                    "}";

            String billnoListStr = k3CloudApi.executeBillQueryJson(billNoQuery);

            // 二 、获取到单号，查询明细
            JsonNode billnoList = objectMapper.readTree(billnoListStr);
            String jbillno= billnoList.get(0).get(0).asText();
            log.info("采购入库单号为-:{}",jbillno);

            //调用接口明细
            String billDataQuery= "{\"CreateOrgId\":0,\"Number\":\""+jbillno+"\",\"Id\":\"\",\"IsSortBySeq\":\"false\"}";
            String resultJson = k3CloudApi.view("STK_InStock",billDataQuery);


            // 三 、用于记录结果处理返回
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));

                //objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
                // 读整个单据信息
                JsonNode billInfo = objectMapper.readTree(gson.toJson(repoRet.getResult().getResult()));
                JsonNode billEntryCol=billInfo.get("InStockEntry");
                JsonNode billEntry= billEntryCol.get(0);


                // 构建返回结构
                //ObjectNode rtobj = objectMapper.createObjectNode();
                rtobj.put("Date",billInfo.get("Date").asText());
                rtobj.put("brand",billEntry.get("F_gggg_Text13").asText());
                rtobj.put("OwnerName",billEntry.get("F_gggg_Text5").asText());
                rtobj.put("model",billEntry.get("F_gggg_Text13").asText());
                rtobj.put("name",billEntry.get("MaterialId").get("Name").get(0).get("Value").asText());
                rtobj.put("types",billEntry.get("F_gggg_Text11").asText());

                rtobj.put("unitno",String.valueOf(invresult11.get(0).get(3)));
                rtobj.put("unitname",String.valueOf(invresult11.get(0).get(4)));
                rtobj.put("qty",String.valueOf(invresult11.get(0).get(5)));


            // 状态取入库单上的不会跟新，现在去集市库存的。
            //int wtatus= CarStatusEnum.getCodeByValue(billEntry.get("StockStatusId").get("Number").asText());
      //          int wtatus= CarStatusEnum.getCodeByValue(String.valueOf(invresult.get(0).get(1)));


            } else {
                log.info("接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return R.ok(rtobj,"");
    }


    @PostMapping("/car_change")
    @Operation(summary = "4按批次监销变更")
    public R setCarChange(@RequestBody Map<String,String> jsonStirng){

        String lotno = jsonStirng.get("sheet_code");
        String status = jsonStirng.get("status");
        String newstatus = jsonStirng.get("newstatus");

        ObjectNode rtobj = objectMapper.createObjectNode();
        try {
            //按照 批号 和 库存状态 未检销 查找即时库存
            String invQuery = "{\n" +
                    "    \"FormId\": \"STK_Inventory\",\n" +
                    "    \"FieldKeys\": \"FMaterialId.fnumber,FMaterialName," +
                    "         FStockUnitId.fnumber,FStockUnitId.fname," +
                    "         FStockId.fnumber,FStockId.fname," +
                    "         FStockStatusId.fnumber,FStockStatusId.fname," +
                    "            FModel,FBaseQty,FLot.fnumber\",\n" +
                    "    \"FilterString\": \"FLot.fnumber='"+lotno+"' and FStockStatusId.fnumber='"+status+"' and FBASEQTY !=0\",\n" +
                    "    \"OrderString\": \"\",\n" +
                    "    \"TopRowCount\": 0,\n" +
                    "    \"StartRow\": 0,\n" +
                    "    \"Limit\": 0\n" +
                    "}";
            var result = k3CloudApi.executeBillQuery(invQuery);

            // 结果转成对象集合
            List<InvVo> invVos=result.stream().map(inv->
                    new InvVo(
                            Convert.toStr(inv.get(0)),Convert.toStr(inv.get(1)),
                            Convert.toStr(inv.get(2)),Convert.toStr(inv.get(3)),
                            Convert.toStr(inv.get(4)),Convert.toStr(inv.get(5)),
                            Convert.toStr(inv.get(6)), Convert.toStr(inv.get(7)),
                            Convert.toStr(inv.get(8)), Convert.toStr(inv.get(9)),
                            Convert.toStr(inv.get(10))
                    )).toList();

            if (invVos.size()<1)
                return R.failed("","没有查询到数据");



            // 三 、调用状态转换单接口,//库存状态转换单,用于记录结果处理返回
            //对返回结果进行解析和校验
            RepoRet repoRetSave = gson.fromJson(k3CloudApi.save("STK_StockConvert"," {\n" +
                    "       \"NeedUpDateFields\": [],\n" +
                    "       \"NeedReturnFields\": [],\n" +
                    "       \"IsDeleteEntry\": \"true\",\n" +
                    "       \"SubSystemId\": \"\",\n" +
                    "       \"IsVerifyBaseDataField\": \"false\",\n" +
                    "       \"IsEntryBatchFill\": \"true\",\n" +
                    "       \"ValidateFlag\": \"true\",\n" +
                    "       \"NumberSearch\": \"true\",\n" +
                    "       \"IsAutoAdjustField\": \"false\",\n" +
                    "       \"IsAutoSubmitAndAudit\":\"true\",\n" +
//                    自动提交审核
//                    来源：金蝶云社区
//                    原文链接：https://vip.kingdee.com/article/509455783696820480?lang=zh-CN&productLineId=1&isKnowledge=2
                    "       \"InterationFlags\": \"\",\n" +
                    "       \"IgnoreInterationFlag\": \"\",\n" +
                    "       \"Model\": {\n" +
                    "           \"FID\": 0,\n" +
                    "           \"FBillTypeID\": {\n" +
                    "               \"FNUMBER\": \"KCZTZH01_SYS\"\n" +
                    "           },\n" +
                    "           \"FStockOrgId\": {\n" +
                    "               \"FNumber\": \"100\"\n" +
                    "           },\n" +
                    "           \"FDate\": \""+LocalDate.now().toString()+"\",\n" +
                    "           \"FOwnerTypeIdHead\": \"BD_OwnerOrg\",\n" +
                    "           \"FOwnerIdHead\": {\n" +
                    "               \"FNumber\": \"100\"\n" +
                    "           },\n" +
                    "           \"FConvertReason\": \"1\",\n" +
                    "           \"FEntity\": [\n" +
                    "               {\n" +
                    "                   \"FConvertType\": \"A\",\n" +
                    "                   \"FMaterialId\": {\n" +
                    "                       \"FNumber\": \""+invVos.get(0).getMaterialNo()+"\"\n" +
                    "                   },\n" +
                    "                   \"FUnitID\": {\n" +
                    "                       \"FNumber\": \""+invVos.get(0).getUnitNo()+"\"\n" +
                    "                   },\n" +
                    "                   \"FConvertQty\": "+invVos.get(0).getQty()+",\n" +
                    "                   \"FStockId\": {\n" +
                    "                       \"FNumber\": \""+invVos.get(0).getStockNo()+"\"\n" +
                    "                   },\n" +
                    "                   \"FStockStatus\": {\n" +
                    "                       \"FNumber\": \""+status+"\"\n" +
                    "                   },\n" +
                    "                   \"FLot\": {\n" +
                    "                       \"FNumber\": \""+invVos.get(0).getLot()+"\"\n" +
                    "                   },\n" +
                    "                   \"FKeeperTypeId\": \"BD_KeeperOrg\",\n" +
                    "                   \"FKeeperId\": {\n" +
                    "                       \"FNumber\": \"100\"\n" +
                    "                   },\n" +
                    "                   \"FOwnerTypeId\": \"BD_OwnerOrg\",\n" +
                    "                   \"FOwnerId\": {\n" +
                    "                       \"FNumber\": \"100\"\n" +
                    "                   }\n" +
                    "               },\n" +
                    "               {\n" +
                    "                   \"FConvertType\": \"B\",\n" +
                    "                   \"FMaterialId\": {\n" +
                    "                       \"FNumber\": \""+invVos.get(0).getMaterialNo()+"\"\n" +
                    "                   },\n" +
                    "                   \"FUnitID\": {\n" +
                    "                       \"FNumber\": \""+invVos.get(0).getUnitNo()+"\"\n" +
                    "                   },\n" +
                    "                   \"FConvertQty\": "+invVos.get(0).getQty()+",\n" +
                    "                   \"FStockId\": {\n" +
                    "                       \"FNumber\": \""+invVos.get(0).getStockNo()+"\"\n" +
                    "                   },\n" +
                    "                   \"FStockStatus\": {\n" +
                    "                       \"FNumber\": \""+newstatus+"\"\n" +
                    "                   },\n" +
                    "                   \"FLot\": {\n" +
                    "                       \"FNumber\": \""+invVos.get(0).getLot()+"\"\n" +
                    "                   },\n" +
                    "                   \"FKeeperTypeId\": \"BD_KeeperOrg\",\n" +
                    "                   \"FKeeperId\": {\n" +
                    "                       \"FNumber\": \"100\"\n" +
                    "                   },\n" +
                    "                   \"FOwnerTypeId\": \"BD_OwnerOrg\",\n" +
                    "                   \"FOwnerId\": {\n" +
                    "                       \"FNumber\": \"100\"\n" +
                    "                   }\n" +
                    "               }\n" +
                    "           ]\n" +
                    "       }\n" +
                    "   }"), RepoRet.class);

            // 保存成功后的处理
            if (repoRetSave.getResult().getResponseStatus().isIsSuccess()) {
                // 拿到单号
                String billno=repoRetSave.getResult().getResponseStatus().getSuccessEntitys().get(0).getNumber();
                RepoRet repoRetSubmeit = gson.fromJson(
                        k3CloudApi.submit("STK_StockConvert","{\n" +
                        "  \"CreateOrgId\": 0,\n" +
                        "  \"Numbers\": [\""+billno+"\"],\n" +
                        "  \"Ids\": \"\",\n" +
                        "  \"SelectedPostId\": 0,\n" +
                        "  \"UseOrgId\": 0,\n" +
                        "  \"NetworkCtrl\": \"\",\n" +
                        "  \"IgnoreInterationFlag\": \"\"\n" +
                        "}"),
                        RepoRet.class);

                // 提交成功后的处理
                if (repoRetSubmeit.getResult().getResponseStatus().isIsSuccess()) {
                    RepoRet repoRetAudit = gson.fromJson(
                            k3CloudApi.audit("STK_StockConvert","{\n" +
                                    "  \"CreateOrgId\": 0,\n" +
                                    "  \"Numbers\": [\""+billno+"\"],\n" +
                                    "  \"Ids\": \"\",\n" +
                                    "  \"InterationFlags\": \"\",\n" +
                                    "  \"UseOrgId\": 0,\n" +
                                    "  \"NetworkCtrl\": \"\",\n" +
                                    "  \"IsVerifyProcInst\": \"true\",\n" +
                                    "  \"IgnoreInterationFlag\": \"\",\n" +
                                    "  \"UseBatControlTimes\": \"false\"\n" +
                                    "}"),
                            RepoRet.class);
                    if (repoRetAudit.getResult().getResponseStatus().isIsSuccess()) {
                        rtobj.put("billno",billno);
                        return R.ok(rtobj);
                    }
                    else{
                        k3CloudApi.delete("STK_StockConvert","{\n" +
                                "  \"CreateOrgId\": 0,\n" +
                                "  \"Numbers\": [\""+billno+"\"],\n" +
                                "  \"Ids\": \"\",\n" +
                                "  \"NetworkCtrl\": \"\"\n" +
                                "}");
                        return R.failed("",gson.toJson(repoRetAudit.getResult().getResponseStatus().getErrors()));
                    }

                }else{
                    k3CloudApi.delete("STK_StockConvert","{\n" +
                            "  \"CreateOrgId\": 0,\n" +
                            "  \"Numbers\": [\""+billno+"\"],\n" +
                            "  \"Ids\": \"\",\n" +
                            "  \"NetworkCtrl\": \"\"\n" +
                            "}");
                    return R.failed("",gson.toJson(repoRetSubmeit.getResult().getResponseStatus().getErrors()));
                }

            } else {
                return R.failed("",gson.toJson(repoRetSave.getResult().getResponseStatus().getErrors()));
            }
        } catch (Exception e) {
            return R.failed("",e.getMessage());
        }
    }


    //@PostMapping("/car_part_info")
    @PostMapping("/material_inv_info")
    @Operation(summary = "5配件库查询--已修改为通用的库存查询")
    public R getCarpartInfo(@RequestBody Map<String,String> jsonStirng) {

        String stockcode = jsonStirng.get("stockcode");
        String itemcode = jsonStirng.get("itemcode");
        String Stockstatus = jsonStirng.get("Stockstatus");

        List<InvVo> invVoList;
        try {
            //按照 批号 和 库存状态 未检销 查找即时库存
            String fieldStr = "{\n" +
                    "    \"FormId\": \"STK_Inventory\",\n" +
                    "    \"FieldKeys\": \"FMaterialId.fnumber,FMaterialName," +
                    "         FStockUnitId.fnumber,FStockUnitId.fname," +
                    "         FStockId.fnumber,FStockId.fname," +
                    "         FStockStatusId.fnumber,FStockStatusId.fname," +
                    "            FModel,FBaseQty,FLot.fnumber\",\n" +
                    "    \"FilterString\": \"";


            String filterStr="FBASEQTY !=0" ;
            if (stockcode!=null)
            {
                filterStr=filterStr+" and FStockId.FNumber='"+stockcode+"'" ;
            }
            if (itemcode!=null)
            {
                filterStr=filterStr+" and FMaterialId.FNumber='"+itemcode+"'";
            }
            if (Stockstatus!=null)
            {

                filterStr=filterStr+" and FStockStatusId.fnumber='"+Stockstatus+"'";
            }

            String orderStr="\",\n    \"OrderString\": \"\",\n" +
                    "    \"TopRowCount\": 0,\n" +
                    "    \"StartRow\": 0,\n" +
                    "    \"Limit\": 0\n" +
                    "}";

            String invQueryStr=fieldStr+filterStr+orderStr;
            var result = k3CloudApi.executeBillQuery(invQueryStr);


            // 结果转成对象集合
            invVoList=result.stream()
                    .filter(f->(Convert.toBigDecimal(f.get(9))).compareTo(BigDecimal.ZERO)!=0)
                    .map(m->
                            new InvVo(
                                    Convert.toStr(m.get(0)), Convert.toStr(m.get(1)),
                                    Convert.toStr(m.get(2)), Convert.toStr(m.get(3)),
                                    Convert.toStr(m.get(4)), Convert.toStr(m.get(5)),
                                    Convert.toStr(m.get(6)), Convert.toStr(m.get(7)),
                                    Convert.toStr(m.get(8)), Convert.toStr(m.get(9)), Convert.toStr(m.get(10))
                            )
                    ).toList();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return R.ok(invVoList,"");

    }


    @PostMapping("/car_new_part")
    @Operation(summary = "6拆解车配件新增接口")
    public R addCarNewPart(@RequestBody Map<String,Object> jsonStirng)  {

        R r=new R<>();

        try {
            JsonNode jsonNodepda = objectMapper.readTree(objectMapper.writeValueAsString(jsonStirng));


            //按照 批号 和 库存状态 未检销 查找即时库存
            String savemaf = "{\n" +
                    "    \"Model\": {\n" +
                    "        \"FID\": 0,\n" +
                    "        \"FBillType\": {\n" +
                    "            \"FNUMBER\": \"JDSCRK01_SYS\"\n" +
                    "        },\n" +
                    "        \"FDate\": \""+LocalDate.now().toString()+"\",\n" +
                    "        \"FStockOrgId\": {\n" +
                    "            \"FNumber\": \"100\"\n" +
                    "        },      \n" +
                    "        \"FTransferBizTypeId\": {\n" +
                    "            \"FNumber\": \"OverOrgPrdIn\"\n" +
                    "        },\n" +
                    "        \"F_xjlh_Assistant\": {\n" + //加工车辆
                    "            \"FNumber\": \""+jsonNodepda.get("F_xjlh_Assistant").asText()+"\"\n" +
                    "        },\n" +
                    "        \"F_xjlh_Assistant1\": {\n" +
                    "            \"FNumber\": \""+jsonNodepda.get("F_xjlh_Assistant1").asText()+"\"\n" +//加工单位
                    "        },\n" +
                    "        \"FEntity\": [\n";

            JsonNode FEntity=jsonNodepda.get("FEntity");
            for (JsonNode entity:FEntity) {
                savemaf=savemaf+ " {\n" +
                        "                \"FMaterialId\": {\n" +
                        "                    \"FNumber\": \""+entity.get("MaterialCode").asText()+"\"\n" +
                        "                },\n" +
                        "                \"FInStockType\": \"1\",\n" +
                        "                \"FUnitID\": {\n" +
                        "                    \"FNumber\": \""+entity.get("UnitCode").asText()+"\"\n" +
                        "                },\n" +
                        "                \"FMustQty\": "+entity.get("Qty")+",\n" +
                        "                \"FRealQty\": "+entity.get("Qty")+",\n" +
                        "                \"FStockId\": {\n" +
                        "                    \"FNumber\": \""+entity.get("StockCode").asText()+"\"\n" +
                        "                },\n" +
                        "                \"FWorkShopId1\": {\n" +
                        "                    \"FNumber\": \""+entity.get("WorkShopCode").asText()+"\"\n" +
                        "                },\n" +
                        "                \"FIsAffectCost\": false,\n" +
                        "                \"FOwnerTypeId\": \"BD_OwnerOrg\",\n" +
                        "                \"FOwnerId\": {\n" +
                        "                    \"FNumber\": \"100\"\n" +
                        "                },\n" +
//                        "                \"FStockUnitId\": {\n" +
//                        "                    \"FNumber\": \""+entity.get("Qty")+"\"\n" +
//                        "                },\n" +
//                        "                \"FStockRealQty\": "+entity.get("Qty")+",\n" +
                        "                \"FStockStatusId\": {\n" +
                        "                    \"FNumber\": \"KCZT01_SYS\"\n" +
                        "                }\n" +
                        "            }," ;
            }

            savemaf=savemaf.substring(0, savemaf.length()-1);
            savemaf=savemaf+
                    "        ]\n" +
                    "    }\n" +
                    "}";


            var result = k3CloudApi.save("SP_InStock",savemaf);

            // 三 、用于记录结果处理返回
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(result, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));
                // 读整个单据信息
//                JsonNode billInfo = objectMapper.readTree(gson.toJson(repoRet.getResult().getResult()));
//                JsonNode billEntryCol=billInfo.get("InStockEntry");
//                JsonNode billEntry= billEntryCol.get(0);
                ObjectNode rtobj = objectMapper.createObjectNode();
                rtobj.put("billno",repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getNumber());
                r=R.ok(rtobj,"");

            } else {
                log.info("接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
                r=R.failed(null,gson.toJson(repoRet.getResult().getResponseStatus()));
            }
            //JsonNode billnoList = objectMapper.readTree(billnoListStr);
            //String jbillno= billnoList.get(0).get(0).asText();
        }  catch (JsonProcessingException e) {
            r=R.failed(null,e.getMessage());
        } catch (Exception e) {
            r=R.failed(null,e.getMessage());
        }

        return r;

    }


    @PostMapping("/transfer")
    @Operation(summary = "7调拨单")
    public R addTransfer(@RequestBody Map<String,Object> jsonStirng)  {

        R r=new R<>();

        try {
            // 读整个单据信息
            JsonNode billInfo = objectMapper.readTree(gson.toJson(jsonStirng));
            JsonNode billEntryCol = billInfo.get("FEntity");



            //按照订单编号查找订单明细
            String MafStr = "{\n" +
                    "\n" +
                    "    \"Model\": {  \n" +
                    "        \"FBillTypeID\": {\n" +
                    "            \"FNUMBER\": \"ZJDB01_SYS\"\n" +
                    "        },\n" +
                    "        \"FBizType\": \"NORMAL\",\n" +
                    "        \"FTransferDirect\": \"GENERAL\",\n" +
                    "        \"FTransferBizType\": \"InnerOrgTransfer\",\n" +
                    "        \"FStockOrgId\": {\n" +
                    "            \"FNumber\": \"100\"\n" +
                    "        },\n" +
                    "        \"FDate\": \""+billInfo.get("FDate").asText()+"\",\n" +
                    "        \"F_xjlh_Assistant\": {\n" +
                    "            \"FNumber\": \"008\"\n" +
                    "        },\n" +
                    "        \"F_xjlh_Assistant1\": {\n" +
                    "            \"FNumber\": \"010\"\n" +
                    "        },\n" +
                    "        \"F_xjlh_Assistant11\": {\n" +
                    "            \"FNumber\": \"002\"\n" +
                    "        },\n" +
                    "        \"FBillEntry\": [\n" +
                    "            {\n" +
                    "                \"FMaterialId\": {\n" +
                    "                    \"FNumber\": \""+billInfo.get("FMaterialId").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FDestMaterialId\": {\n" +
                    "                    \"FNUMBER\": \""+billInfo.get("FMaterialId").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FUnitID\": {\n" +
                    "                    \"FNumber\": \""+billInfo.get("FUnitID").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FQty\": "+billInfo.get("FQty").asText()+",\n" +
                    "                \"FLot\": {\n" +
                    "                    \"FNumber\": \""+billInfo.get("FLot").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FDestLot\": {\n" +
                    "                    \"FNumber\": \""+billInfo.get("FLot").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FSrcStockId\": {\n" +
                    "                    \"FNumber\": \""+billInfo.get("FSrcStockId").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FDestStockId\": {\n" +
                    "                    \"FNumber\": \""+billInfo.get("FDestStockId").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FSrcStockStatusId\": {\n" +
                    "                    \"FNumber\": \""+"KCZT01_SYS"+"\"\n" +
                    "                },\n" +
                    "                \"FDestStockStatusId\": {\n" +
                    "                    \"FNumber\": \""+"KCZT01_SYS"+"\"\n" +
                    "                }\n" +
                    "            }\n" +
                    "        ]\n" +
                    "    }\n" +
                    "}";


            var resultJson = k3CloudApi.save("STK_TransferDirect",MafStr);

            // 三 、用于记录结果处理返回
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));


                // 新组装的数据// 构建返回结构
                ObjectNode rtInfo = objectMapper.createObjectNode();
                rtInfo.put("BillNo", repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getNumber());

                r = R.ok(rtInfo, null);
            }
            else{
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));

                // 读整个单据信息
                JsonNode rtbillInfo = objectMapper.readTree(gson.toJson(repoRet.getResult().getResponseStatus().getErrors()));

                // 新组装的数据// 构建返回结构
                ObjectNode rtInfo = objectMapper.createObjectNode();
                rtInfo.put("Error", rtbillInfo);


                r = R.failed(rtInfo, null);
            }

        }  catch (JsonProcessingException e) {
            r=R.failed(null,e.getMessage());
        } catch (Exception e) {
            r=R.failed(null,e.getMessage());
        }

        return r;

    }


    @PostMapping("/sppro_outstock")
    @Operation(summary = "8车辆简单领料")
    public R addSpout(@RequestBody Map<String,Object> jsonStirng)  {

        R r=new R<>();

        try {
            // 读整个单据信息
            JsonNode billInfo = objectMapper.readTree(gson.toJson(jsonStirng));
            JsonNode billEntryCol = billInfo.get("FEntity");



            //按照订单编号查找订单明细
            String MafStr = "{\n" +
                    "\n" +
                    "    \"Model\": {\n" +
                    "        \"FID\": 0,\n" +
                    "        \"FBillType\": {\n" +
                    "            \"FNUMBER\": \"JDSCLL01_SYS\"\n" +
                    "        },\n" +
                    "        \"FDate\": \""+billInfo.get("FDate").asText()+"\",\n" +
                    "        \"FStockOrgId\": {\n" +
                    "            \"FNumber\": \"100\"\n" +
                    "        },\n" +
                    "        \n" +
                    "        \"FPrdOrgId\": {\n" +
                    "            \"FNumber\": \"100\"\n" +
                    "        },\n" +
                    "        \"FWorkShopId\": {\n" +
                    "            \"FNumber\": \"BM000009\"\n" +
                    "        },\n" +
                    "        \"FTransferBizTypeId\": {\n" +
                    "            \"FNumber\": \"OverOrgPick\"\n" +
                    "        },\n" +
                    "        \"FBizType\": \"NORMAL\",\n" +
                    "        \"F_xjlh_Assistant\": {\n" +
                    "            \"FNumber\": \"007\"\n" +
                    "        },\n" +
                    "        \"FEntity\": [\n" +
                    "            {\n" +
                    "                \"FMaterialId\": {\n" +
                    "                    \"FNumber\": \""+billInfo.get("FMaterialId").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FUnitID\": {\n" +
                    "                    \"FNumber\": \""+billInfo.get("FUnitID").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FAppQty\": "+billInfo.get("FAppQty").asText()+",\n" +
                    "                \"FActualQty\": "+billInfo.get("FAppQty").asText()+",\n" +
                    "                \"FStockId\": {\n" +
                    "                    \"FNumber\": \""+billInfo.get("FStockId").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FLot\": {\n" +
                    "                    \"FNumber\": \""+billInfo.get("FLot").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FStockStatusId\": {\n" +
                    "                    \"FNumber\": \""+billInfo.get("FStockStatusId").asText()+"\"\n" +
                    "                },\n" +
                    "                \"FPRODUCTGROUPID\": {\n" +
                    "                    \"FNUMBER\": \"CPZ000001\"\n" +
                    "                }\n" +
                    "            }\n" +
                    "        ]\n" +
                    "    }\n" +
                    "}";

            var resultJson = k3CloudApi.save("SP_PickMtrl",MafStr);

            // 三 、用于记录结果处理返回
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));


                // 新组装的数据// 构建返回结构
                ObjectNode rtInfo = objectMapper.createObjectNode();
                rtInfo.put("BillNo", repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getNumber());

                r = R.ok(rtInfo, null);
            }
            else{
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));

                // 读整个单据信息
                JsonNode rtbillInfo = objectMapper.readTree(gson.toJson(repoRet.getResult().getResponseStatus().getErrors()));

                // 新组装的数据// 构建返回结构
                ObjectNode rtInfo = objectMapper.createObjectNode();
                rtInfo.put("Error", rtbillInfo);


                r = R.failed(rtInfo, null);
            }

        }  catch (JsonProcessingException e) {
            r=R.failed(null,e.getMessage());
        } catch (Exception e) {
            r=R.failed(null,e.getMessage());
        }

        return r;

    }


    @PostMapping("/mfg_order_list")
    @Operation(summary = "9生产订单列表")
    public R getMfgOrderList(@RequestBody Map<String,String> jsonStirng)  {

        String stockcode = jsonStirng.get("mfgOrder");
        R r;
        try {

            //按照订单状态查找订单列表，--备用，有可能会用上
            var queryMafOrderStr = k3CloudApi.executeBillQuery("{\n" +
                    "    \"FormId\": \"PRD_MO\",\n" +
                    //"    \"FieldKeys\": \"FBILLNO,FDate,FMaterialId.FNumber,FMaterialId.FName,Fqty\",\n" +
                    "    \"FieldKeys\": \"FBILLNO,FDate\",\n" +
                    "    \"FilterString\": \"FDocumentStatus='C' and FStatus='4'\",\n" +
                    "    \"OrderString\": \"\",\n" +
                    "    \"TopRowCount\": 0,\n" +
                    "    \"StartRow\": 0,\n" +
                    "    \"Limit\": 0\n" +
                    "}");



            r = R.ok(queryMafOrderStr, null);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return r;

    }


    //"Manufacture"一词常简写为"Mfg."，代表制造或生产活动。
    @PostMapping("/mfg_order_info")
    @Operation(summary = "10生产订单明细")
    public R getMfgOrder(@RequestBody Map<String,Object> jsonStirng)  {

        R r=new R<>();

        try {
            JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(jsonStirng));
            String mfgOrder= jsonNode.get("mfgOrder").asText();

            //按照订单状态查找订单列表，--备用，有可能会用上
//            String queryMafOrderStr = "{\n" +
//                    "    \"FormId\": \"PRD_MO\",\n" +
//                    "    \"FieldKeys\": \"FBILLNO,FDate,FTreeEntity_FEntryId,FMaterialId.FNumber\",\n" +
//                    "    \"FilterString\": \"FDocumentStatus='C' and FStatus='4'\",\n" +
//                    "    \"OrderString\": \"\",\n" +
//                    "    \"TopRowCount\": 0,\n" +
//                    "    \"StartRow\": 0,\n" +
//                    "    \"Limit\": 0\n" +
//                    "}";

            //按照订单编号查找订单明细
            String queryMafOrderStr = "{\n" +
                    "    \"CreateOrgId\": 0,\n" +
                    "    \"Number\": \""+mfgOrder+"\",\n" +
                    "    \"Id\": \"\"\n" +
                    "}";
            var resultJson = k3CloudApi.view("PRD_MO",queryMafOrderStr);

            // 三 、用于记录结果处理返回
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);

            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));

                // 读整个单据信息
                JsonNode billInfo = objectMapper.readTree(gson.toJson(repoRet.getResult().getResult()));
                JsonNode billEntryCol = billInfo.get("TreeEntity");

                // 新组装的数据// 构建返回结构
                ObjectNode rtInfo = objectMapper.createObjectNode();
                rtInfo.put("Date", billInfo.get("Date").asText());
                rtInfo.put("BillNo", billInfo.get("BillNo").asText());
                rtInfo.put("MoId", billInfo.get("Id").asText());
                ArrayNode rtEntrysInfo = rtInfo.putArray("TreeEntity");
                for (JsonNode entry:billEntryCol) {
                    ObjectNode EntryInfo=objectMapper.createObjectNode();
                    EntryInfo.put("Id", entry.get("Id").asText());
                    EntryInfo.put("Seq", entry.get("Seq").asText());
                    EntryInfo.put("MaterialNumber", entry.get("MaterialId").get("Number").asText());
                    EntryInfo.put("MaterialName", entry.get("MaterialId").get("Name").get(0).get("Value").asText());
                    EntryInfo.put("Specification", entry.get("MaterialId").get("Specification").get(0).get("Value").asText());
                    EntryInfo.put("Qty", entry.get("Qty").asText());
                    EntryInfo.put("UnitNumber", entry.get("UnitId").get("Number").asText());
                    EntryInfo.put("UnitName", entry.get("UnitId").get("Name").get(0).get("Value").asText());
                    //EntryInfo.put("SrcBillType", entry.get("SrcBillType").get("Name").get(0).get("Value").asText());
                    EntryInfo.put("SrcBillNumber", entry.get("SaleOrderNo").asText());
                    EntryInfo.put("NoStockInQty", entry.get("NoStockInQty").asText());

                    int moCode=entry.get("Status").asInt();
                    EntryInfo.put("Status", MoStatusEnum.getValue(moCode));


                    rtEntrysInfo.add(EntryInfo);
                }

                r = R.ok(rtInfo, null);
            }

            //JsonNode billnoList = objectMapper.readTree(billnoListStr);
            //String jbillno= billnoList.get(0).get(0).asText();
        }  catch (JsonProcessingException e) {
            r=R.failed(null,e.getMessage());
        } catch (Exception e) {
            r=R.failed(null,e.getMessage());
        }

        return r;

    }


    @Deprecated
    @PostMapping("/brick_new1")
    @Operation(summary = "11生产入库")
    public R MfgInStock1(@RequestBody Map<String,Object> jsonStirng)  {

        R r=new R<>();

        try {
            // 读整个单据信息
            JsonNode billInfo = objectMapper.readTree(gson.toJson(jsonStirng));
            JsonNode billEntryCol = billInfo.get("FEntity");


            //按照订单编号查找订单明细
            String MafStr = "{\n" +
                    "    \"Model\": {\n" +
                    "        \"FDate\": \""+billInfo.get("FDate").asText()+"\",\n" +
                    "        \"FStockOrgId\": {\n" +
                    "            \"FNumber\": \"100\"\n" +
                    "        },\n" +
                    "        \"FPrdOrgId\": {\n" +
                    "            \"FNumber\": \"100\"\n" +
                    "        },\n" +
                    "        \"FOwnerTypeId0\": \"BD_OwnerOrg\",\n" +
                    "        \"FOwnerId0\": {\n" +
                    "            \"FNumber\": \"100\"\n" +
                    "        },\n" +
                    "        \"FEntity\": [\n";



            for (JsonNode entry:billEntryCol) {

                MafStr=MafStr+  "            {\n" +
                        "                \"FMaterialId\": {\n" +
                        "                    \"FNumber\": \""+entry.get("MaterialNumber").asText()+"\"\n" +
                        "                },\n" +
                        "                \"FInStockType\": \"1\",\n" +
                        "                \"FProductType\": \"2\",\n" +
                        "                \"FMustQty\": "+entry.get("Qty").asText()+",\n" +
                        "                \"FStockId\": {\n" +
                        "                    \"FNumber\": \""+entry.get("StockNumber").asText()+"\"\n" +
                        "                },\n" +
//                        "                \"FUnitID\": {\n" +
//                        "                    \"FNumber\": \""+entry.get("Unit").asText()+"\"\n" +
//                        "                },\n" +
                        "                \"FStockStatusId\": {\n" +
                        "                    \"FNumber\": \""+entry.get("StockStatusNumber").asText()+"\"\n" +
                        "                },\n" +


                        "                \"FMoBillNo\": \""+entry.get("MoBillNo").asText()+"\",\n" +
                        "                \"FMoId\": "+entry.get("MoId").asLong()+",\n" +
                        "                \"FMoEntryId\": "+entry.get("MoEntryId").asLong()+",\n" +
                        "                \"FMoEntrySeq\": "+entry.get("MoEntrySeq").asInt()+",\n" +

                        "                \"FSrcType\": \"PRD_MO\",\n" +
                        "                \"FSrcBillNo\": \""+entry.get("MoBillNo").asText()+"\","+
                        "                \"FEntity_Link\": [\n" +
                        "                    {\n" +
                        "                        \"FEntity_Link_FRuleId\": \"PRD_MO2INSTOCK\",\n" +
                        "                        \"FEntity_Link_FSTableName\": \"T_PRD_MOENTRY\",\n" +
                        "                        \"FEntity_Link_FSBillId\": \""+entry.get("MoId").asLong()+"\",\n" +
                        "                        \"FEntity_Link_FSId\": \""+entry.get("MoEntryId").asInt()+"\"\n" +
                        "                    }\n" +
                        "                ]"+
                        "            }," ;
            }

            MafStr=MafStr.substring(0, MafStr.length()-1);
            MafStr=MafStr+  "        ]\n" +
                    "    }\n" +
                    "}";

            var resultJson = k3CloudApi.save("PRD_INSTOCK",MafStr);



            // 三 、用于记录结果处理返回
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));


                // 新组装的数据// 构建返回结构
                ObjectNode rtInfo = objectMapper.createObjectNode();
                rtInfo.put("BillNo", repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getNumber());

                r = R.ok(rtInfo, null);
            }
            else{
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));

                // 读整个单据信息
                JsonNode rtbillInfo = objectMapper.readTree(gson.toJson(repoRet.getResult().getResponseStatus().getErrors()));

                // 新组装的数据// 构建返回结构
                ObjectNode rtInfo = objectMapper.createObjectNode();
                rtInfo.put("Error", rtbillInfo);


                r = R.ok(rtInfo, null);
            }

        }  catch (JsonProcessingException e) {
            r=R.failed(null,e.getMessage());
        } catch (Exception e) {
            r=R.failed(null,e.getMessage());
        }

        return r;

    }


    /// 用下退的方案解决，放到服务端解决了，因为api下推必须有必填项，否则机会滚了
    @PostMapping("/brick_new")
    @Operation(summary = "11生产入库")
    public R MfgInStock(@RequestBody Map<String,Object> jsonStirng)  {

        R r=new R<>();

        try {
            Map<String,Map<String,Object>> kdparameter=new HashMap<>();
            //kdparameter.put("kdparameter",jsonStirng);


            String rt=k3CloudApi.execute("ZHT.SCM.PRO.WebApiServices.ScmApiServices.PrdMoPushByService,ZHT.SCM.PRO.WebApiServices",new Object[]{jsonStirng});

            r=objectMapper.readValue(rt,R.class);
        } catch (Exception e) {
            r=R.failed(null,e.getMessage());
        }

        return r;

    }



    @PostMapping("/sale_order_list")
    @Operation(summary = "12销售订单列表")
    public R getSaleOrderList(@RequestBody Map<String,String> jsonStirng)  {

        String saleOrder = jsonStirng.get("saleOrder");
        R r;
        try {
            var queryMafOrderStr = k3CloudApi.executeBillQuery("{\n" +
                    "    \"FormId\": \"SAL_SaleOrder\",\n" +
                    //"    \"FieldKeys\": \"FBILLNO,FDate,FMaterialId.FNumber,FMaterialId.FName,Fqty\",\n" +
                    "    \"FieldKeys\": \"FBILLNO,FDate\",\n" +
                    "    \"FilterString\": \"FDocumentStatus='C' and FCloseStatus='A'\",\n" +
                    "    \"OrderString\": \"\",\n" +
                    "    \"TopRowCount\": 0,\n" +
                    "    \"StartRow\": 0,\n" +
                    "    \"Limit\": 0\n" +
                    "}");
            r = R.ok(queryMafOrderStr, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return r;

    }


    //"Manufacture"一词常简写为"Mfg."，代表制造或生产活动。
    @PostMapping("/sale_order_info")
    @Operation(summary = "13销售订单")
    public R getSaleOrder(@RequestBody Map<String,Object> jsonStirng)  {

        R r=new R<>();

        try {
            JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(jsonStirng));
            String saleOrder= jsonNode.get("saleOrder").asText();

            //按照订单状态查找订单列表，--备用，有可能会用上
//            String queryMafOrderStr = "{\n" +
//                    "    \"FormId\": \"PRD_MO\",\n" +
//                    "    \"FieldKeys\": \"FBILLNO,FDate,FTreeEntity_FEntryId,FMaterialId.FNumber\",\n" +
//                    "    \"FilterString\": \"FDocumentStatus='C' and FStatus='4'\",\n" +
//                    "    \"OrderString\": \"\",\n" +
//                    "    \"TopRowCount\": 0,\n" +
//                    "    \"StartRow\": 0,\n" +
//                    "    \"Limit\": 0\n" +
//                    "}";

            //按照订单编号查找订单明细
            String queryMafOrderStr = "{\n" +
                    "    \"CreateOrgId\": 0,\n" +
                    "    \"Number\": \""+saleOrder+"\",\n" +
                    "    \"Id\": \"\"\n" +
                    "}";
            var resultJson = k3CloudApi.view("SAL_SaleOrder",queryMafOrderStr);

            // 三 、用于记录结果处理返回

            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));

                // 读整个单据信息
                JsonNode billInfo = objectMapper.readTree(gson.toJson(repoRet.getResult().getResult()));
                JsonNode billEntryCol = billInfo.get("SaleOrderEntry");

                // 新组装的数据// 构建返回结构
                ObjectNode rtInfo = objectMapper.createObjectNode();
                rtInfo.put("Date", billInfo.get("Date").asText());
                rtInfo.put("BillNo", billInfo.get("BillNo").asText());
                rtInfo.put("Id", billInfo.get("Id").asText());
                rtInfo.put("CustNumber", billInfo.get("CustId").get("Number").asText());
                rtInfo.put("CustName", billInfo.get("CustId").get("Name").get(0).get("Value").asText());
                ArrayNode rtEntrysInfo = rtInfo.putArray("SaleOrderEntry");
                for (JsonNode entry:billEntryCol) {
                    ObjectNode EntryInfo=objectMapper.createObjectNode();
                    EntryInfo.put("Id", entry.get("Id").asText());
                    EntryInfo.put("Seq", entry.get("Seq").asText());
                    EntryInfo.put("MaterialNumber", entry.get("MaterialId").get("Number").asText());
                    EntryInfo.put("MaterialName", entry.get("MaterialId").get("Name").get(0).get("Value").asText());
                    EntryInfo.put("Specification", entry.get("MaterialId").get("Specification").get(0).get("Value").asText());
                    EntryInfo.put("Qty", entry.get("Qty").asText());
                    EntryInfo.put("UnitNumber", entry.get("UnitId").get("Number").asText());
                    EntryInfo.put("UnitName", entry.get("UnitId").get("Name").get(0).get("Value").asText());

                    rtEntrysInfo.add(EntryInfo);
                }

                r = R.ok(rtInfo, null);
            }

            //JsonNode billnoList = objectMapper.readTree(billnoListStr);
            //String jbillno= billnoList.get(0).get(0).asText();
        }  catch (JsonProcessingException e) {
            r=R.failed(null,e.getMessage());
        } catch (Exception e) {
            r=R.failed(null,e.getMessage());
        }

        return r;

    }


    @PostMapping("/brick_out")
    @Operation(summary = "14销售出库")
    public R addSaleOutStock(@RequestBody Map<String,Object> jsonStirng)  {

        R r=new R<>();

        try {
            // 读整个单据信息
            JsonNode billInfo = objectMapper.readTree(gson.toJson(jsonStirng));
            JsonNode billEntryCol = billInfo.get("FEntity");


            //按照订单编号查找订单明细
            String MafStr = "{\n" +
                    "    \"Model\": {\n" +
                    "        \"FDate\": \""+billInfo.get("FDate").asText()+"\",\n" +
                    "        \"FSaleOrgId\": {\n" +
                    "            \"FNumber\": \"100\"\n" +
                    "        },\n" +
                    "        \"FCorrespondOrgId\": {\n" +
                    "            \"FNumber\": \"100\"\n" +
                    "        },\n" +
                    "        \"FCustomerID\": {\n" +
                    "            \"FNumber\": \""+billInfo.get("CustomerNumber").asText()+"\"\n" +
                    "        },\n" +
                    "        \"FStockOrgId\": {\n" +
                    "            \"FNumber\": \"100\"\n" +
                    "        },\n" +
                    "        \"FSaleDeptID\": {\n" +
                    "            \"FNumber\": \"ZHB\"\n" +
                    "        },\n" +
                    "        \"FSalesManID\": {\n" +
                    "            \"FNumber\": \"113_ZHB012_1\"\n" +
                    "        },\n" +
                    "        \"F_xjlh_Assistant\": {\n" +
                    "            \"FNumber\": \"008\"\n" +
                    "        },\n" +
                    "        \"FEntity\": [";



            for (JsonNode entry:billEntryCol) {

                MafStr=MafStr+  "            {\n" +
                        "                \"FMaterialID\": {\n" +
                        "                    \"FNumber\": \""+entry.get("MaterialNumber").asText()+"\"\n" +
                        "                },\n" +
                        "\n" +
                        "                \"FRealQty\": "+entry.get("Qty").asLong()+",\n" +
                        "                \"FStockID\": {\n" +
                        "                    \"FNumber\": \""+entry.get("StockNumber").asText()+"\"\n" +
                        "                },\n" +

                        "                \"FStockStatusId\": {\n" +
                        "                    \"FNumber\": \""+entry.get("StockStatusNumber").asText()+"\"\n" +
                        "                },\n" +

                        "                \"F_xjlh_Qty4\": "+entry.get("Qty").asLong()+",\n" +
                        "                \"F_xjlh_Assistant2\": {\n" +
                        "                    \"FNumber\": \"004\"\n" +
                        "                },\n" +


                "                \"FSOEntryId\": \""+entry.get("SoEntryId").asInt()+"\",\n" +

                        "                \"FSrcType\": \"SAL_SaleOrder\",\n" +
                        "                \"FSrcBillNo\": \""+entry.get("SoBillNo").asText()+"\","+
                        "                \"FEntity_Link\": [\n" +
                        "                    {\n" +
                        "                        \"FEntity_Link_FRuleId\": \"SaleOrder-OutStock\",\n" +
                        "                        \"FEntity_Link_FSTableName\": \"T_SAL_ORDERENTRY\",\n" +
                        "                        \"FEntity_Link_FSBillId\": \""+entry.get("SoId").asLong()+"\",\n" +
                        "                        \"FEntity_Link_FSId\": \""+entry.get("SoEntryId").asInt()+"\",\n" +

                        "                        \"FEntity_Link_FBaseUnitQtyOld\": \""+entry.get("Qty").asLong()+"\",\n" +
                        "                        \"FEntity_Link_FBaseUnitQty\": \""+entry.get("Qty").asLong()+"\",\n" +
                        "                        \"FEntity_Link_FSALBASEQTYOld\": \""+entry.get("Qty").asLong()+"\",\n" +
                        "                        \"FEntity_Link_FSALBASEQTY\": \""+entry.get("Qty").asLong()+"\",\n" +
                        "                        \"FEntity_Link_FAuxUnitQtyOld\": \""+entry.get("Qty").asLong()+"\",\n" +
                        "                        \"FEntity_Link_FAuxUnitQty\": \""+entry.get("Qty").asLong()+"\",\n" +
                        "                        \"FEntity_Link_FLnk1Amount\": \""+entry.get("Qty").asLong()+"\"\n" +

                        "                    }\n" +
                        "                ]},";
            }




            MafStr=MafStr.substring(0, MafStr.length()-1);
            MafStr=MafStr+  "        ]\n" +
                    "    }\n" +
                    "}";

            var resultJson = k3CloudApi.save("SAL_OUTSTOCK",MafStr);

            // 三 、用于记录结果处理返回
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));


                // 新组装的数据// 构建返回结构
                ObjectNode rtInfo = objectMapper.createObjectNode();
                rtInfo.put("BillNo", repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getNumber());

                r = R.ok(rtInfo, null);
            }else{
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));

                // 读整个单据信息
                JsonNode rtbillInfo = objectMapper.readTree(gson.toJson(repoRet.getResult().getResponseStatus().getErrors()));

                // 新组装的数据// 构建返回结构
                ObjectNode rtInfo = objectMapper.createObjectNode();
                rtInfo.put("Error", rtbillInfo);


                r = R.ok(rtInfo, null);
            }

        }  catch (JsonProcessingException e) {
            r=R.failed(null,e.getMessage());
        } catch (Exception e) {
            r=R.failed(null,e.getMessage());
        }

        return r;

    }




    @GetMapping("test")
    @Operation(summary = "测试")
    public String testnoauth() {
        return "ok";
    }

}


